import request from '@/utils/request'

/**
 * 查询订单列表
 * @param {Object} query - 查询参数
 * @param {number} [query.pageNum] - 页码
* @param {number} [query.pageSize] - 每页数量
* @param {string} [query.orderSn] - 订单编号
* @param {number} [query.status] - 订单状态
* @returns {Promise} 返回订单列表数据
 */
export function listOrder(query) {
  return request({
    url: '/merchant/order/list',
    method: 'get',
    params: query
  })
}

/**
 * 获取订单详细信息
 * @param {string|number} param - 订单编号或订单ID
 * @returns {Promise} 返回订单详细信息
 */
export function getOrder(param) {
  const isOrderSn = typeof param === 'string'
  return request({
    url: isOrderSn ? `/merchant/order/sn/${param}` : `/merchant/order/${param}`,
    method: 'get'
  })
}

/**
 * 创建订单
* @param {Object} data - 订单信息
* @returns {Promise} 返回创建结果
 */
export function createOrder(data) {
  return request({
    url: '/merchant/order',
    method: 'post',
    data: data
  })
}

/**
 * 修改订单
* @param {Object} data - 订单信息
* @returns {Promise} 返回修改结果
 */
export function updateOrder(data) {
  return request({
    url: '/merchant/order',
    method: 'put',
    data: data
  })
}

/**
 * 删除订单
* @param {number|number[]} ids - 订单ID或ID数组
* @returns {Promise} 返回删除结果
 */
export function deleteOrder(ids) {
  return request({
    url: `/merchant/order/${typeof ids === 'object' ? ids.join(',') : ids}`,
    method: 'delete'
  })
}

/**
 * 订单支付
* @param {string} orderSn - 订单编号
* @param {number} payType - 支付方式(1-支付宝，2-微信)
* @returns {Promise} 返回支付结果
 */
export function payOrder(orderSn, payType = 1) {
  return request({
    url: `/merchant/order/pay/sn/${orderSn}`,
    method: 'post',
    params: { payType }
  })
}

/**
 * 确认收货
* @param {number} orderId - 订单ID
* @returns {Promise} 返回确认结果
 */
export function confirmReceiveOrder(orderId) {
  return request({
    url: `/merchant/order/receive/${orderId}`,
    method: 'put'
  })
}

/**
 * 取消订单
* @param {string} orderSn - 订单编号
* @returns {Promise} 返回取消结果
 */
export function cancelOrder(orderSn) {
  return request({
    url: `/merchant/order/cancel/sn/${orderSn}`,
    method: 'put'
  })
}

/**
 * 查看物流信息
* @param {number} orderId - 订单ID
* @returns {Promise} 返回物流信息
 */
export function getOrderLogistics(orderId) {
  return request({
    url: `/merchant/order/logistics/${orderId}`,
    method: 'get'
  })
}

/**
 * 订单发货
 * @param {number} orderId - 订单ID
 * @param {string} deliveryCompany - 物流公司
 * @param {string} deliverySn - 物流单号
 * @returns {Promise} 返回发货结果
 */
export function shipOrder(orderId, deliveryCompany, deliverySn) {
  return request({
    url: `/merchant/order/delivery/${orderId}`,
    method: 'post',
    params: {
      deliveryCompany,
      deliverySn
    }
  })
}

/**
 * 更新订单备注
 * @param {Object} data - 订单信息
 * @returns {Promise} 返回更新结果
 */
export function updateOrderNote(data) {
  return request({
    url: '/merchant/order/note',
    method: 'put',
    data: data
  })
}

/**
 * 关闭订单
 * @param {number} orderId - 订单ID
 * @param {string} note - 关闭原因
 * @returns {Promise} 返回关闭结果
 */
export function closeOrder(orderId, note) {
  return request({
    url: `/merchant/order/cancel/${orderId}`,
    method: 'put',
    params: {
      note
    }
  })
}

/**
 * 导出订单
 * @param {Object} query - 查询参数
 * @returns {Promise} 返回导出结果
 */
export function exportOrder(query) {
  return request({
    url: '/merchant/order/export',
    method: 'post',
    responseType: 'blob',
    params: query
  })
} 